const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const app = getApp()
Page({
  data: {
    openUpload: false,//上传路径选择
    uploadType: '',//上传的是正面还是背面
    openSubmit: false,//是否填写完整开启上传
    openProtocol: false,//协议面板
    openSubmitPanel: false,//确认提交面板
    info: { "name": "", "phone": "", "weChat": "", "idNumber": "", "frontPhoto": "", "reversePhoto": "", "auditUnderway": 0 }//auditUnderway 认证状态 0 准备认证 1 认证中 2 认证通过 3 重新认证（认证失败和认证到期） 
  },
  onLoad: function (options) {

  },
  /**
   * 输入信息
   */
  inputInfo: function(e){
    var _this = this;
    var type = e.currentTarget.dataset.type;
    var text = e.detail.value.replace(/\s+/g, '');
    if (type == "name") {
      _this.setData({
        ["info.name"]: text
      })
    }
    if (type == "phone") {
      _this.setData({
        ["info.phone"]: text
      })
    }
    if (type == "weChat") {
      _this.setData({
        ["info.weChat"]: text
      })
    }
    if (type == "idNumber") {
      _this.setData({
        ["info.idNumber"]: text
      })
    }
    _this.checkIdNumber();
  },
  /**
   * 效验姓名
   */
  checkName: function(){
    var _this = this;
    var name = _this.data.info.name
    if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(name))) {
      _this.setData({
        ["info.name"]: ''
      })
      wx.showToast({
        title: '姓名有误',
        duration: 1500,
        icon: 'none'
      });
    }
    _this.checkIdNumber();
  },
  /**
   * 效验手机号
   */
  checkPhone: function(){
    var _this = this;
    var phone = _this.data.info.phone
    if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(phone))) {
      _this.setData({
        ["info.phone"]: ''
      })
      wx: wx.showToast({
        title: '请输入正确的电话',
        icon: 'none',
        duration: 1500
      })
    }
    _this.checkIdNumber();
  },
  /**
   * 效验身份证
   */
  checkIdNumber: function () {
    var _this = this;
    var idNumber = _this.data.info.idNumber
    if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idNumber))) {
      _this.setData({
        ["info.idNumber"]: ''
      })
      wx.showToast({
        title: '身份证号码有误',
        duration: 1500,
        icon: 'none'
      });
    }
    _this.checkIdNumber();
  },
  /**
   * 效验是否填写完整
   */
  checkIdNumber: function () {
    var _this = this;
    var info = _this.data.info
    if (info.name != '' && info.name != null && info.phone != '' && info.phone != null && info.weChat != '' && info.weChat != null && info.idNumber != '' && info.idNumber != null && info.frontPhoto != '' && info.frontPhoto != null && info.reversePhoto != '' && info.reversePhoto != null ){
      _this.setData({
        openSubmit: true
      })
    }else{
      _this.setData({
        openSubmit: false
      })
    }
  },
  /**
   * 打开上传路径选择面板
   */
  showUpload: function(e){
    var _this = this;
    var type = e.currentTarget.dataset.type;
    _this.setData({
      openUpload: true,
      uploadType: type
    })
    _this.checkIdNumber();
  },
  //打开拍摄
  openShoot: function () {
    var _this = this;
    var type = _this.data.uploadType;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['camera'],
      success(res) {
        _this.closeMC();
        if (type =="front"){
          _this.setData({
            ["info.frontPhoto"]: res.tempFilePaths[0]
          })
        }
        if (type =="reverse"){
          _this.setData({
            ["info.reversePhoto"]: res.tempFilePaths[0]
          })
        }
        _this.checkIdNumber();
      }
    })
  },
  //打开相册
  openAlbum: function () {
    var _this = this
    var type = _this.data.uploadType;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success(res) {
        _this.closeMC();
        if (type == "front") {
          _this.setData({
            ["info.frontPhoto"]: res.tempFilePaths[0]
          })
        }
        if (type == "reverse") {
          _this.setData({
            ["info.reversePhoto"]: res.tempFilePaths[0]
          })
        }
        _this.checkIdNumber();
      }
    })
  },
  /**
   * 打开认证协议面板
   */
  showProtocol: function () {
    var _this = this;
    _this.setData({
      openProtocol: true,
    })
    _this.checkIdNumber();
  },
  /**
   * 打开确认提交面板
   */
  showSubmitPanel: function () {
    var _this = this;
    _this.setData({
      openSubmitPanel: true,
    })
  },
  /**
   * 关闭面板
   */
  closeMC: function(){
    var _this = this;
    _this.setData({
      openUpload: false,
      openProtocol: false,
      openSubmitPanel: false,
    })
  },
  /**
   * 提交审核
   */
  submitInfo: function(){
    var _this = this;
    _this.closeMC();
    var info = _this.data.info
    wx.showToast({
      title: '提交成功',
      duration: 2000,
      icon: 'success',
      success: function () {
        _this.setData({
          ["info.auditUnderway"]: 1
        })
      }
    });
  }
})